package net.skyedu.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.SysCourse;
import net.skyedu.dao.DaoSupport;

public class Pagination extends HttpServlet {
	private static final long serialVersionUID = 1L;


	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
              doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
		  	
		  	int currentpage = Integer.parseInt(request.getParameter("currentPage"));
	  		int pagesize = Integer.parseInt(request.getParameter("pageSize"));
	  		String term = request.getParameter("term");
	  			term = URLDecoder.decode(term,"UTF-8");
	  		String subject = request.getParameter("subject");
	  			subject = URLDecoder.decode(subject,"UTF-8");
	  		String grade = request.getParameter("grade");
	  			grade = URLDecoder.decode(grade,"UTF-8");
	  		String level = request.getParameter("level");
	  			level = URLDecoder.decode(level,"UTF-8");
	  		
	  		int startIndex = 0;
	  		try{
	        	  startIndex = (currentpage - 1) * pagesize;
	        	  DaoSupport ds = new DaoSupport();
			      StringBuffer jsonAttr = new StringBuffer("[");
			      int pageCount = 0;
	        	  List<SysCourse> list = ds.list(startIndex, pagesize, 1, term, subject, grade, level);
	        	  pageCount = ds.list(startIndex, pagesize, 0, term, subject, grade, level).size();
        		  if(pageCount != 0){
    			      for(int i = 0; i < list.size(); i++){
    			    	  SysCourse sc = list.get(i);
    			    	  jsonAttr.append("{");
    			    	  jsonAttr.append("\"couId\":\""+sc.getCouId()+"\",");
    			    	  jsonAttr.append("\"year\":\""+sc.getYear()+"\",");
    			    	  jsonAttr.append("\"term\":\""+sc.getTerName()+"\",");
    			    	  jsonAttr.append("\"subName\":\""+sc.getSubName()+"\",");
    			    	  jsonAttr.append("\"graName\":\""+sc.getGraName()+"\",");
    			    	  jsonAttr.append("\"claLevel\":\""+sc.getClaLevel()+"\",");
    			    	  jsonAttr.append("\"claNum\":\""+sc.getClaNumber()+"\",");
    			    	  jsonAttr.append("\"perunitFee\":\""+sc.getPerunitFee()+"\",");
    			    	  jsonAttr.append("\"bookFee\":\""+sc.getBookFee()+"\",");
    			    	  jsonAttr.append("\"predictNum\":\""+sc.getPredictNum()+"\",");
    			    	  jsonAttr.append("\"claType\":\""+sc.getClaType()+"\",");
    			    	  jsonAttr.append("\"bookVersion\":\""+sc.getBookVersion()+"\",");

//    			    	  String content = sc.getClaContent();
//    			    	  StringBuffer subContent = new StringBuffer();
//    			    	  if(content != null && !"".equals(content)){
//    			    		  if(content.length() > 5){
//    			    			  subContent.append(content.substring(0,5)).append("..");
//    			    		  }else{
//    			    			  subContent.append(content);
//    			    		  }
//    			    	  }
//    			    	  jsonAttr.append("\"subContent\":\""+subContent+"\",");
//    			    	  jsonAttr.append("\"claContent\":\""+sc.getClaContent()+"\",");
    			    	  
    			    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); //记录总数
    			    	  
    			    	  jsonAttr.append("}");
    			    	  if(i<list.size()-1){
    			    		  jsonAttr.append(",");
    			    	  }
    			       }
    		      }else{
    		    	  jsonAttr.append("{");
    		    	  jsonAttr.append("\"pageCount\":\""+pageCount+"\""); 
    		    	  jsonAttr.append("}");
    		      }
        	  
		      jsonAttr.append("]");
//		      System.out.println("JASON:---"+jsonAttr);
		      PrintWriter out = response.getWriter();
		      out.print(jsonAttr);
		      out.flush();
		      out.close();
        	
          }catch(Exception e){
        	  //写到日志表中
        	  e.printStackTrace();
          }
		
		
		
		
	
	}
	
	
	
	

	public void init() throws ServletException {}

}
